/**
 * <p><b>Title:</b> InitDataHisController.java </p>
 * @Package com.wondertek.poms.init.controller
 */
package com.wondertek.poms.init.controller;

import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.init.po.InitDataHis;
import com.wondertek.poms.init.service.IInitDataHisService;
import com.wondertek.poms.init.vo.InitDataHisVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p><b>InitDataHisController Description:</b> 初始化历史表 Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2020-03-30 18:57:15</p>
 */
@Api("初始化历史 ")
@RestController
@Scope("prototype")
@RequestMapping(value="/initDataHis")
public class InitDataHisController extends BaseController {
	
	@Autowired
	private IInitDataHisService initDataHisService;
	
	@ApiOperation("初始化历史 列表")
	@GetMapping("/list")
	@ResponseBody
	public ResultBean list(HttpServletRequest request,
			InitDataHisVo initDataHisVo,
			@RequestParam(defaultValue="1") int page, 
			@RequestParam(defaultValue="20") int rows,
			@RequestParam(defaultValue="updatedTime") String sidx, 
			@RequestParam(defaultValue="desc") String sord) {
		InitDataHis initDataHis = new InitDataHis();
		BeanUtils.copyProperties(initDataHisVo, initDataHis);
		
		Page<InitDataHis> pages = initDataHisService
				.findAll(initDataHis, page, rows, sord, sidx);
		
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation("初始化历史 新增")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "uuid", required = true),
		@ApiImplicitParam(name = "platform", required = true),
		@ApiImplicitParam(name = "operation", required = true),
		@ApiImplicitParam(name = "contentId", required = true),
		@ApiImplicitParam(name = "strategyCodes", required = true),
		@ApiImplicitParam(name = "isMishitong", required = true),
		@ApiImplicitParam(name = "accessPlatformId", required = true),
		@ApiImplicitParam(name = "createTime", required = true),
		@ApiImplicitParam(name = "status", required = true),
		@ApiImplicitParam(name = "des", required = true),
	})
	@PostMapping(value = "/add")
	@ResponseBody
	//TODO (需要删除不需要的属性列)
	public ResultBean add(HttpServletRequest request, InitDataHisVo initDataHisVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(initDataHisVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(initDataHisVo)) 
			return ResultBean.error(MessageCodes.get(MessageCodes.M980001));
	
		InitDataHis initDataHis = new InitDataHis();
		BeanUtils.copyProperties(initDataHisVo, initDataHis);
		
		initDataHisService.save(initDataHis);
		return ResultBean.ok(initDataHis);
	}
	
	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断初始化历史表 名称是否重复</p>
	 * @author douzi
	 * @param initDataHisVo
	 */
	private boolean isRepeat(InitDataHisVo initDataHisVo) {
		InitDataHis temp = new InitDataHis();
		// temp.setChannelName(initDataHisVo.getName());
		Optional<InitDataHis> initDataHisOpt = initDataHisService.findOne(Example.of(temp));
		if (initDataHisOpt.isPresent()) {
			InitDataHis initDataHis = initDataHisOpt.get();
			if (initDataHisVo.getId() != null && initDataHisVo.getId() != initDataHis.getId())
				return true;
			else if (initDataHisVo.getId() == null)
				return true;
		}
		return false;
	}
	
	@ApiOperation("初始化历史 详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@ResponseBody
	public ResultBean get(Long id) {
		InitDataHis initDataHis = initDataHisService.get(id);
		return ResultBean.ok(initDataHis);
	}
	
	@ApiOperation("初始化历史 变更")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", required = true),
		@ApiImplicitParam(name = "uuid", required = true),
		@ApiImplicitParam(name = "platform", required = true),
		@ApiImplicitParam(name = "operation", required = true),
		@ApiImplicitParam(name = "contentId", required = true),
		@ApiImplicitParam(name = "strategyCodes", required = true),
		@ApiImplicitParam(name = "isMishitong", required = true),
		@ApiImplicitParam(name = "accessPlatformId", required = true),
		@ApiImplicitParam(name = "createTime", required = true),
		@ApiImplicitParam(name = "status", required = true),
		@ApiImplicitParam(name = "des", required = true),
	})
	@PutMapping(value = "/edit")
	@ResponseBody
	//TODO (需要删除不需要的属性列)
	public ResultBean update(HttpServletRequest request, InitDataHisVo initDataHisVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(initDataHisVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		
		if (isRepeat(initDataHisVo)) 
			return ResultBean.error(MessageCodes.get(MessageCodes.M980001));
		
		InitDataHis initDataHis = new InitDataHis();
		BeanUtils.copyProperties(initDataHisVo, initDataHis);
		return ResultBean.ok(initDataHisService.merge(initDataHis));
	}
	
	@ApiOperation("初始化历史 删除")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(Long id) {
		initDataHisService.deleteById(id);
		return ResultBean.ok();
	}
}
